package com.lihan.controller;

import com.lihan.common.Result;
import com.lihan.model.bo.UserBo;
import com.lihan.deepseek.DeepSeekClient;
import com.lihan.model.dto.LoginByPhoneForm;
import com.lihan.model.dto.UserLoginForm;
import com.lihan.model.entity.User;
import com.lihan.services.IUserService;
import com.lihan.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理模块")
public class UserController {
    @Resource
    private IUserService userService;

    @Resource
    private DeepSeekClient deepSeekClient;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result login(UserLoginForm user) throws IOException {
        Result result = userService.login(user);
        return result;
    }

    @PostMapping("/loginByPhone")
    @ApiOperation(value = "使用手机号登录")
    public Result loginByPhone(LoginByPhoneForm form) throws IOException {
        Result result = userService.loginByPhone(form);
        return result;
    }

    @PostMapping("/register")
    @ApiOperation(value = "表单注册")
    public Result register(@RequestBody Map<String,String> userForm) throws Exception {
            // 转化成User.class
            String userJson = userForm.get("user");
            User userInfo = JsonUtils.jsonToPojo(userJson, User.class); //将前端的数据转化为User.class

            // 注册
            Result result = userService.register(userInfo);
            return result;
    }

    @PutMapping("/updateNickName")
    @ApiOperation(value = "修改用户昵称")
    public Result setNickName(User user){
       User userResult =  userService.updateUserInfo(user);
       return Result.ok(userResult);
    }

    @PostMapping("/uploadFaceBase64")
    @ApiOperation(value = "修改用户头像")
    public Result uploadImage(@RequestBody UserBo userBo) throws Exception {

        Result result = userService.updateFaceImage(userBo);
        return result;
    }

    @GetMapping("/deepseek")
    public Result deepseek(String content){

        List<Result> msg = new ArrayList<>();
        msg.add(new Result(deepSeekClient.start(content)));
        return  Result.ok(msg);
    }
}
